﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using VonaJobs.DAL.DAO;

namespace VonaJobs.DAL.Gatway
{
    public class Servicegateway:DBGateway
    {
        public string SaveNewService(Service service)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText =
                    string.Format("insert into tblService values(@Subject,@Description,@MemberTypeId)");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@Subject", SqlDbType.NVarChar).Value = service.Subject;
                CommandObj.Parameters.Add("@Description", SqlDbType.NVarChar).Value = service.Description;
                CommandObj.Parameters.Add("@MemberTypeId", SqlDbType.Int).Value = service.MemberType.Id;
                CommandObj.ExecuteNonQuery();
                return "New service created successfully";
            }
            catch (Exception)
            {
                throw new Exception("Message Not Send, Server Error !");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }

            }
        }

        public List<Service> GetAllServices()
        {
            List<Service> services=new List<Service>();
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format("select * from tblServices");
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    Service service=new Service();
                    MemberType memberType=new MemberType();
                    service.MemberType = memberType;
                    service.Id = Convert.ToInt32(reader[0]);
                    service.MemberType.TypeName = reader[1].ToString();

                }
                return services;


            }
            catch (Exception)
            {

                throw new Exception("Can not read service data, Server Error !");
            }
        }

    }
}